<?php
class paginator
{
	private $limit;
	private $num;
	private $query;	
	function __construct($str,$n)
	{
		$this->limit=$n;
		$this->query=$str;
		$this->num=mysql_num_rows(mysql_query($str));
		
	}	
	function paginate_query()
	{
		$n=$this->limit;
		if(!isset($_GET['a']) || $_GET['a']==0)
		{
			$a=0;
		}
		else 
		{
			$a=$_GET['a'];	
		}
		$str=$this->query." limit $a,$n";
		$result=mysql_query($str);
		return $result;
	}
	function paginate_show()
	{	
		$x=0;
		$p=1;
		$prev=0;
		if(isset($_GET['a']))
		{
			$next=$_GET['a'];
			$prev=$_GET['a'];
			$a=$_GET['a'];
		}
		else 
		{
	    	$next=0;
			$prev=0;
			$a=0;	
		}
		echo "<div id='paginate'>";
		$prev=$prev-$this->limit;
		echo "<b><a class='prev-next' href='?a=$x'>first</a></b>";
		if($prev>=0)
		{
			echo "<a href='?a=$prev' class='prev-next'>prev</a>";
		}
		while($x<$this->num)
		{
			$curr=$a/$this->limit;
			$curr=$curr+1;
			if($p==$curr)
			{		
				 echo "<b><a class='pagenow' href='?a=$x'>$p</a></b>";
			}
			else 
			{
			   echo "<a href='?a=$x'>$p</a>";
			}
		    $p++;
			$x=$x+$this->limit;
			
		}
		$next=$next+$this->limit;
		if($next<$this->num)
		{
			echo "<a href='?a=$next' class='prev-next'>next</a>";
		}
		$x=$x-$this->limit;
		echo "<b><a class='prev-next' href='?a=$x'>last</a></b>";	
		echo "</div>";	
	}
}
?>